<?php
include_once "lib/config.php";
include_once "header.php";


if(isset($_POST['save'])&& $_POST['save']!="")
{
	$data= $_POST['Category'];
    $data['last_modified_date']=time();
    $data['last_modified_by']=$_SESSION['u_id'];
    
    
	if(isset($_POST['cat_id']) && $_POST['cat_id']>0)
	{
	    $check_query=DB::updatecheckExistance("af_technocategories",array("cat_name"=>$data['cat_name'],"type"=>$data['type']),array("id"=>$_POST['cat_id']));
        
		if($check_query==0)
		{
		    $categorydetail=DB::fetch_array(DB::query("select cat_name from af_technocategories where id='".$_POST['cat_id']."'"));
            $insertdataarr=array("staff_id"=>$_SESSION['u_id'],"action"=>"Updated the Technopath category ".$categorydetail[0]['cat_name'],"logtime"=>time());
            DB::insertData("stafflog",$insertdataarr);
            
		    DB::updateData("af_technocategories",$data,array("id"=>$_POST['cat_id']));
				
			if(DB::$affectedrows>0)
			{
			    
				$_SESSION['msg_success']="Category updated successfully.";
                redirect('technocategories.php');
			}
			else
			{
				$msg_error="There is no record updated.";
			}
		}
		else
		{
			$msg_error="There is already one record exists for given category name.";
		}
	}
	else
	{
		$check_query=DB::checkExistance("af_technocategories",array("cat_name"=>$data['cat_name'],"type"=>$data['type']));
        
		if($check_query==0)
		{
			$data['cat_status']="Active";
			
            $insertdataarr=array("staff_id"=>$_SESSION['u_id'],"action"=>"Inserted the Technopath category ".$data['cat_name'],"logtime"=>time());
            DB::insertData("stafflog",$insertdataarr);
            
            DB::insertData("af_technocategories",$data);
			
            if(DB::$affectedrows>0)
			{
			 
				$_SESSION['msg_success']="Category added successfully.";
				redirect('technocategories.php');
			}
			else
			{
				$msg_error="There is no record added.";
			}
			
		}
		else
		{
			$msg_error="There is already one record exists for given category name.";
		}
	}
	
}
$dob=date("d/m/Y");
if(isset($_POST['cat_id']) && $_POST['cat_id']>0)
{
	$conditionarr=array(":id"=>$_POST['cat_id']);
    $categoryinfo=DB::fetch_array(DB::query("select * from af_technocategories where id=:id",$conditionarr));
    
    if(isset($categoryinfo[0]))
    {
        $data=$categoryinfo[0];
    }
    else
    {
        $data=array();
    }
}

?>
  		<div class="container_12" id="main_div">
           <form action="" method="post" onsubmit="return validateaddeditcategory();" enctype="multipart/form-data">
			<br>
			<div class="grid_12" >
		    	<?php
            	if(isset($msg_success) && $msg_success!="")
            	{
            	?>
                <div>
                    <span class="notification n-success"><?php echo $msg_success; ?></span>
                </div>
                <?php
                }
                if(isset($msg_error) && $msg_error!="")
                {
                ?>
                <span class="notification n-error"><?php echo $msg_error; ?></span>
                <?php
				}
                ?>
		        <div class="module" >
		        		
		             <h2><span onclick="toggle_div('basic_body','toogleimg');" ><img src="images/desc.gif" height="5px" id="toogleimg">&nbsp;&nbsp;Technopath Category Details</span></h2>
		                
		             <div class="module-body" id="basic_body">
		                
		                  	<p>
		                        <label>Category Name<span style="color: red;">*</span></label>
		                        <input type="text" class="input-short" style="width: 280px;" id="name" name="Category[cat_name]" value="<?php if(isset($data['cat_name']) && $data['cat_name']!=Null)echo $data['cat_name']; ?>"/>
		                        
		                    </p>
		                    
		             </div> <!-- End .module-body -->
		
		        </div>  <!-- End .module -->
				
		    </div>
		    
		    <div class="grid_12">
		    <fieldset>
		    	&nbsp;
		    	<input type="hidden" id="cat_id" name="cat_id" value="<?php if(isset($_POST['cat_id'])){ echo $_POST['cat_id']; } ?>">
		        <input class="submit-green" type="submit" name="save" value="Submit" /> 
		        <input class="submit-gray" type="button" value="Back" onclick="window.location='technocategories.php'" />
		    </fieldset>
		    </div>
		    <div style="clear:both;"></div>
		 </form>    
        </div> <!-- End .container_12 -->
		
<?php include_once("footer.php"); ?>           
<?php
if(isset($msg_success) && $msg_success!="")
{
	$notymessage=$msg_success;
	$msgtype="success";
}
else if(isset($msg_error) && $msg_error!="")
{
	$msgtype="error";
	$notymessage=$msg_error;
}
else
{
	$msgtype="";
}
if($msgtype!="")
{
?>        
<script language="javascript">
	$(document).ready(function(){
		
		var options = $.parseJSON('{"text":"<?php echo $notymessage; ?>","layout":"top","type":"<?php echo $msgtype; ?>"}');
		noty(options);
		
	});
</script>  
<?php
}
?>
<script>

function validateaddeditcategory()
{
	
	if(trim(document.getElementById("name").value)=="")
	{
		alertify.alert("Category Name field required!");
		document.getElementById("name").focus();
		return false;
	}
	
}
</script>       